package com.atom.app.mvi.ui.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.TextField
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import coil.compose.AsyncImage
import com.atom.app.mvi.R
import com.atom.module.logger.Logger
import kotlinx.coroutines.delay

/**
 * https://www.cnblogs.com/guanxinjing/p/16939623.html
 */
class EffectActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {}
    }

    /**
     *  LaunchedEffect
     *  DisposableEffect
     *
     *  rememberCoroutineScope
     *  rememberUpdatedState
     *
     *  produceState
     *  derivedStateOf
     */

    @Composable
    private fun StudyEffect(){
        val owner: LifecycleOwner = LocalLifecycleOwner.current
        val start = {
            Logger.e("start on")
        }
        val end = {
            Logger.e("start on")
        }

        DisposableEffect(key1 = owner, effect = {
            val observer = LifecycleEventObserver { _, event ->
                if (event == Lifecycle.Event.ON_START) {
                    start()
                } else if (event == Lifecycle.Event.ON_STOP) {
                    end()
                }
            }
            owner.lifecycle.addObserver(observer)

            onDispose {
                owner.lifecycle.removeObserver(observer)
            }
        })

        var updatePostData by remember {
            mutableStateOf(true)
        }
        val state =  produceState(initialValue = "请求中", updatePostData) {
            value  = "请求成功"
        }

        Logger.e("produceState  $state")

        Icon(
            painter = painterResource(id = R.mipmap.icon_launcher),
            contentDescription = "",
            tint = Color.Unspecified,
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentWidth(align = Alignment.CenterHorizontally)
                .clickable {
                    updatePostData = !updatePostData
                }
        )
    }

}